#include "InterfaceLayer.h"

#include <imgui.h>
#include <EditApp.h>
#include <Layers/EditLayer.h>
#include <FileSystem/FileSystem.h>
#include <Core/RAII.h>

namespace Edit
{
    void InterfaceLayer::OnImGui()
    {
        const ImGuiViewport* viewport = ImGui::GetMainViewport();
        ImGui::SetNextWindowPos(viewport->WorkPos);
        ImGui::SetNextWindowSize(viewport->WorkSize);

        Engine2D::RAII raii([]() { ImGui::End(); }, []() { ImGui::Begin("Interface", 0, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize); });
        ImGui::SetCursorPos(ImVec2(-100 + ImGui::GetWindowSize().x / 2, -50 + ImGui::GetWindowSize().y / 2));
        if (ImGui::Button("Open Project", ImVec2(100, 100))) {
            auto fileName = Engine2D::FileSystem::Instance().GetFileName(Engine2D::FileSystem::Instance().GetCWD(), "e2d\0*.e2d\0\0");
            if (fileName.empty()) {
                return;
            }
            Engine2D::Project::OpenProject(fileName);
            auto layer = std::make_shared<EditLayer>();
            Engine2D::Application::Instance<EditApp>().lock()->PushBackLayer(layer);
            Engine2D::Application::Instance<EditApp>().lock()->RemoveLayer(this);
        }
        ImGui::SameLine();
        if (ImGui::Button("New Project", ImVec2(100, 100))) {
            auto fileName = Engine2D::FileSystem::Instance().SaveFileName(Engine2D::FileSystem::Instance().GetCWD(), "e2d\0*.e2d\0\0");
            if (fileName.empty()) { 
                return;
            }
            Engine2D::Project::NewProject(fileName);
            Engine2D::Application::Instance<EditApp>().lock()->PushBackLayer(std::make_shared<EditLayer>());
            Engine2D::Application::Instance<EditApp>().lock()->RemoveLayer(this);
        }
        
    }
} // namespace Edit

